package course

import (
	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
	"net/http"
	"strconv"

	"github.com/zeromicro/go-zero/rest/httpx"
	"yangming-college/app/speciality/cmd/v1/api/internal/logic/course"
	"yangming-college/app/speciality/cmd/v1/api/internal/svc"
	"yangming-college/app/speciality/cmd/v1/api/internal/types"
)

func ReviseCourseHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.ReqReviseCourse
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		userId := r.Header.Get("user_id")
		uid, err := strconv.ParseInt(userId, 10, 64)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, errors.Wrapf(err, "speciality v1 api Search error"))
		}
		l := course.NewReviseCourseLogic(r.Context(), svcCtx)
		err = l.ReviseCourse(uid, &req)
		if err != nil {
			logx.Errorf("speciality error", err)
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.Ok(w)
		}
	}
}
